//
//  SheZhiLabelYangShiViewController.swift
//  ZHM
//
//  Created by zzddongge on 16/9/10.
//  Copyright © 2016年 zzd. All rights reserved.
//

import UIKit

protocol SheZhiLabelYangShiViewControllerDelegate : class{
    
    func chuanDiFenZhongZhiDaoXiaoJianPan(_ fenZhongZhi:Int)
    func chuanDiXuanZhongFenZhongZhiDaoXiaoJianPan(_ xuanZhongDeFenZhongValueJieBao:Int)
    func chuanDiIndexPathDianRow(_ indexPathDianRow:Int)
    
    func chuanDiXuanZhongFenZhongValueAndBianKuangXingZhuang(_ xuanZhongFenZhongValue:Int,indexPathDianRow:Int)
    func chuanDiZanTingOrkaiShiStatus(_ zanTingOrKaiShiStatus:Bool)
    
}


class SheZhiLabelYangShiViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,SheZhiZhuTiYangShiDelegate,SheZhiLabelYangShiDelegate,SheZhiLabelXingZhuangDelegate{

    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    //定义UITableView
    var sheZhiLabelYangShi:UITableView?
    
    //定义一个计时器变量
    var timerValue:Int = 0
    
    //接收传递过来的选中的分钟值
    var xuanZhongFenZhongValue:String?
    
    var xuanZhongDeFenZhongValue:String?
    
    //将可选类型的选中的分钟值强制解饱
    var selectedMiniteValue:Int = 0
    
    //定义两个变量
//    var selectedMiniteValue:Int = 0
    var indexPathDianRow:Int = 16
    
    
    //定义代理变量
    weak var delegate:SheZhiLabelYangShiViewControllerDelegate?
    
    var zanTingOrKaiShiStatus:Bool?
    
    
    override func viewDidLoad() {
        
        self.view.backgroundColor = UIColor.white
        self.title = "样式设置"
        print(zanTingOrKaiShiStatus as Any)
        
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
        
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        //修改导航栏按钮颜色
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.barTintColor =
            UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1)
        
        //初始化UITableView
        sheZhiLabelYangShi = UITableView(frame: CGRect(x: 0, y: 15, width: screenWidth, height: screenHeight))
        //设置代理和数据源
        self.sheZhiLabelYangShi?.delegate = self
        self.sheZhiLabelYangShi?.dataSource = self
        
        //初始化iconImageView
//        iconImageView = UIImageView(frame:CGRect(x:10,y:10,width:40,height:40))
        
        print(xuanZhongFenZhongValue as Any)
        self.view.addSubview(sheZhiLabelYangShi!)
        
        super.viewDidLoad()

        
        // Do any additional setup after loading the view.
    }

    /*
     作者:张振东
     时间:2016-9-12
     功能:返回几组
     */
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    
    /*
     作者:张振东
     时间:2016-9-12
     功能:每组显示几个cell
     */
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    /*
     作者:张振东
     时间:2016-9-12
     功能:定义tableViewcell的高度
     */
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "cellIdentifier"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as UITableViewCell?

        if cell == nil{
            cell = UITableViewCell(style: UITableViewCellStyle.default,reuseIdentifier: cellIdentifier)
            cell!.textLabel?.font = UIFont.systemFont(ofSize: 12)
            cell!.selectionStyle = .gray
            cell!.accessoryType = .disclosureIndicator
            tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        //tableViewCell分割线从头开始显示
        tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        switch indexPath.row {
        case 0:
            
            cell!.textLabel?.text = "边框背景色"
            cell?.imageView?.image = UIImage(named:"quweixunlian")
        case 1:
            
            cell?.imageView?.image = UIImage(named:"quweixunlian")
            cell!.textLabel?.text = "边框形状"
        case 2:
            
            cell?.imageView?.image = UIImage(named:"quweixunlian")
            cell!.textLabel?.text = "主题换肤"
        default:
           
            print("--")
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            //跳转到设置边框背景色的界面
            let bianKuangBeiJingSeViewController = BianKuangBeiJingSeViewController()
            bianKuangBeiJingSeViewController.selectedMiniteValue = selectedMiniteValue
            print(xuanZhongDeFenZhongValue as Any)
            bianKuangBeiJingSeViewController.selectedMiniteValue = Int(xuanZhongDeFenZhongValue!)!
            print(Int(xuanZhongDeFenZhongValue!)!)
            print(bianKuangBeiJingSeViewController.selectedMiniteValue)
            bianKuangBeiJingSeViewController.delegate = self
            self.navigationController?.pushViewController(bianKuangBeiJingSeViewController, animated: true)
        case 1:
            //跳转到设置边框形状的界面
            let bianKuangXingZhuangViewController = BianKuangXingZhuangViewController()
            print(xuanZhongDeFenZhongValue as Any)
            bianKuangXingZhuangViewController.selectedMiniteValue = Int(xuanZhongDeFenZhongValue!)!
            print(Int(xuanZhongDeFenZhongValue!)!)
            print(bianKuangXingZhuangViewController.selectedMiniteValue)
            bianKuangXingZhuangViewController.delegate = self
            self.navigationController?.pushViewController(bianKuangXingZhuangViewController, animated: true)
        case 2:
            //跳转到设置主题背景色的界面
            let zhuTiHuanFuViewController = ZhuTiHuanFuViewController()
            zhuTiHuanFuViewController.selectedMiniteValue = selectedMiniteValue
            
            print(xuanZhongDeFenZhongValue as Any)
            zhuTiHuanFuViewController.selectedMiniteValue = Int(xuanZhongDeFenZhongValue!)!
            print(Int(xuanZhongDeFenZhongValue!)!)
            print(zhuTiHuanFuViewController.selectedMiniteValue)
            zhuTiHuanFuViewController.delegate = self
            self.navigationController?.pushViewController(zhuTiHuanFuViewController, animated: true)
        default:
            print("--")
            
        }
    }
    
    //返回至上一个界面
    func backToPrevious(){
        timerValue = UserDefaults.standard.integer(forKey: "timerValue")
        print(timerValue)
        print(xuanZhongDeFenZhongValue as Any)
        print(zanTingOrKaiShiStatus!)
        xuanZhongFenZhongValue = String(selectedMiniteValue)
        delegate?.chuanDiFenZhongZhiDaoXiaoJianPan(timerValue)
        if let xuanZhongDeFenZhongValueJieBao = xuanZhongDeFenZhongValue{
            print(xuanZhongDeFenZhongValueJieBao)
            selectedMiniteValue = Int(xuanZhongDeFenZhongValueJieBao)!
            //将这个值传递到上一个ViewController
            delegate?.chuanDiXuanZhongFenZhongZhiDaoXiaoJianPan(Int(xuanZhongDeFenZhongValueJieBao)!)
            delegate?.chuanDiXuanZhongFenZhongValueAndBianKuangXingZhuang(Int(xuanZhongDeFenZhongValueJieBao)!, indexPathDianRow: indexPathDianRow)
            delegate?.chuanDiIndexPathDianRow(indexPathDianRow)
        
            delegate?.chuanDiZanTingOrkaiShiStatus(zanTingOrKaiShiStatus!)
        }
        print(self.navigationController?.viewControllers as Any)
        self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
    }
    
    
    func chuanDiXuanZhongFenZhongZhiAndBeiJingSe(_ xuanZhongDeFenZhongZhi: Int, xuanZhongBeiJingSeByIndexPathDianRow: Int) {
        print(xuanZhongDeFenZhongZhi)
        print(xuanZhongBeiJingSeByIndexPathDianRow)
        selectedMiniteValue = xuanZhongDeFenZhongZhi
        indexPathDianRow = xuanZhongBeiJingSeByIndexPathDianRow
        backToPrevious()
        
    }
    
    func backToPreviousController(){
        timerValue = UserDefaults.standard.integer(forKey: "timerValue")
        print(timerValue)
        print(xuanZhongDeFenZhongValue as Any)
        xuanZhongFenZhongValue = String(selectedMiniteValue)
        delegate?.chuanDiFenZhongZhiDaoXiaoJianPan(timerValue)
        if let xuanZhongDeFenZhongValueJieBao = xuanZhongDeFenZhongValue{
            print(xuanZhongDeFenZhongValueJieBao)
            selectedMiniteValue = Int(xuanZhongDeFenZhongValueJieBao)!
            //将这个值传递到上一个ViewController
            delegate?.chuanDiXuanZhongFenZhongZhiDaoXiaoJianPan(Int(xuanZhongDeFenZhongValueJieBao)!)
            delegate?.chuanDiXuanZhongFenZhongValueAndBianKuangXingZhuang(Int(xuanZhongDeFenZhongValueJieBao)!, indexPathDianRow: indexPathDianRow)
        }
        print(self.navigationController?.viewControllers as Any)
        self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
    }
    
    
    func chuanDiXuanZhongFenZhongZhiAndXingZhuang(_ xuanZhongDeFenZhongZhi: Int, xuanZhongXingZhuangByIndexPathDianRow: Int) {
        print(xuanZhongDeFenZhongZhi)
        print(xuanZhongXingZhuangByIndexPathDianRow)
        selectedMiniteValue = xuanZhongDeFenZhongZhi
        indexPathDianRow = xuanZhongXingZhuangByIndexPathDianRow
        
        backToPreviousController()
    }
    
    
       override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
